package product

import (
	"gitee.com/tang_q/gin-vue3-admin-api/global"
	"gorm.io/gorm"
)

type Category struct {
	gorm.Model
	Name     string `json:"name" gorm:"column:name;unique"`
	ParentId int    `json:"parent_id" gorm:"column:parent_id"`
}

func init() {
	err := global.DB.AutoMigrate(&Category{})
	if err != nil {
		global.Logger.Error("迁移Category失败", err)
		return
	}
}

func GetBrandCategories() (categories []string, err error) {
	err = global.DB.Model(&Category{}).Select("name").Find(&categories).Error
	return
}

func (g *Category) GetCategoryByName(name string) error {
	return global.DB.Where("name = ?", name).First(g).Error
}
